<?php
namespace app\material\model;
use app\BaseModel;
use app\shop\model\AdminCompany;
use think\facade\Db;



class Brochure extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_material_brochure';

    protected $append = [

        'company'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-07-27 10:58
     * @功能说明:观看次数
     */
    public function getWatchNumAttr($value,$data){

        if(!empty($data['id'])){

            $bIm_model = new BrochureIm();

            $num = $bIm_model->where(['type'=>2,'brochure_id'=>$data['id']])->count();

            return $num;

        }


    }


//    public function getShareTimesAttr($value,$data){
//
//
////        if(!empty($data['id'])){
////
////            $bIm_model = new BrochureShare();
////
////            $num = $bIm_model->where(['type'=>0,'b_id'=>$data['id']])->count();
////
////            return $num;
////
////        }
//    }

    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2021-07-06 15:04
     */
    public function getUseImgsAttr($value,$data){

        if (!empty($value)) {

            return explode(',', $value);

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2021-06-24 16:50
     * @功能说明:公司
     */
    public function getCompanyAttr($value,$data){

        if(!empty($data['id'])&&isset($data['is_company'])&&!empty($data['uniacid'])){

            $company_model = new BrochureCompany();

            if($data['is_company']==1){
                //所有的公司
                $list = $company_model->getCompanyIds(['uniacid'=>$data['uniacid'],'status'=>1]);

            }else{
                //选中的公司
                $dis = [

                    'a.uniacid'    => $data['uniacid'],

                    'a.brochure_id'=> $data['id'],

                    'b.status'      => 1,

                ];

                $list = $company_model->getId($dis);

            }

            return $list;

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-29 16:33
     * @功能说明:
     */
    public function getImgsAttr($value,$data){

        if(!empty($data['imgs'])){

            return explode(',',$data['imgs']);

        }

    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $company = $data['company'];

        unset($data['company']);

        if(isset($data['imgs'])&&is_array($data['imgs'])){

            $data['imgs'] = implode(',',$data['imgs']);

        }else{

            $data['imgs'] = '';
        }

        $file_model = new File();
        //base_64转图片
        if(isset($data['use_imgs'])){

//            $file_arr = $file_model->base64ToPng($data['use_imgs']);
//
//            $data['use_imgs'] = !empty($file_arr)?implode(',',$file_arr):'';

            $data['use_imgs'] = !empty($data['use_imgs'])?implode(',',$data['use_imgs']):'';

            $data = transImages($data,['use_imgs'],',',$data['uniacid']);

            $data['use_imgs'] = !empty($data['use_imgs'])?implode(',',$data['use_imgs']):'';

        }else{

            $data['use_imgs'] = '';
        }

        $res = $this->insert($data);

        $id  = $this->getLastInsID();

        $this->updateSome($id,$data['uniacid'],$company);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-24 15:53
     * @功能说明:添加公司
     */
    public function updateSome($id,$uniacid,$company){

        $company_model = new BrochureCompany();

        $company_model->where(['brochure_id'=>$id])->delete();

        $company = $company_model->changeData($company);

        if(!empty($company)){

            foreach ($company as $value){

                $insert = [

                    'uniacid' => $uniacid,

                    'brochure_id' => $id,

                    'company_id' => $value
                ];

                $company_model->dataAdd($insert);

            }

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['company'])){

            $company = $data['company'];

            unset($data['company']);

            $this->updateSome($dis['id'],$data['uniacid'],$company);
        }

        if(isset($data['imgs'])&&is_array($data['imgs'])){

            $data['imgs'] = implode(',',$data['imgs']);
        }

        if(isset($data['use_imgs'])){

//            $file_model = new File();
//
//            $file_arr = $file_model->base64ToPng($data['use_imgs']);
//
//            $data['use_imgs'] = !empty($file_arr)?implode(',',$file_arr):'';

            $data['use_imgs'] = !empty($data['use_imgs'])?implode(',',$data['use_imgs']):'';

            $data = transImages($data,['use_imgs'],',',$data['uniacid']);

            $data['use_imgs'] = !empty($data['use_imgs'])?implode(',',$data['use_imgs']):'';
        }

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }





    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10,$map=[]){

        $data = $this->alias('a')
                ->join('longbing_card_material_brochure_company b','b.brochure_id = a.id','left')
                ->where($dis)
                ->where(function ($query) use ($map) {
                    $query->whereOr($map);
                })
                ->field('a.*')
                ->group('a.id')
                ->order('a.top desc,a.id desc')
                ->paginate($page)
                ->toArray();


        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-06 13:24
     * @功能说明:分享次数
     */
    public function shareTimeList($dis,$page=10){

//        dump($dis);exit;
        $data = $this->alias('a')
                ->join('longbing_card_material_brochure_share b','a.id = b.b_id','left')
                ->where($dis)
                ->field('a.*,COUNT(a.id) as share_times')
                ->group('a.id')
                ->order('share_times desc,a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-06 13:24
     * @功能说明:分享人数
     */
    public function shareUserList($dis,$page=10){

        $data = $this->alias('a')
            ->join('longbing_card_material_brochure_share b','a.id = b.b_id','left')
            ->where($dis)
            ->field('a.*,COUNT(distinct(b.user_id)) as user_num,COUNT(distinct(b.user_id)) as get_user')
            ->group('a.id')
            ->order('user_num desc,a.id desc')
            ->paginate($page)
            ->toArray();
        return $data;
    }




}